home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5378 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  67 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!milyng
  3. From: mikael@pobox.com (Mikael Lyngvig)
  4. Subject: Re: Help(link error)
  5. Message-ID: <milyngDM8Ho8.JJI@netcom.com>
  6. Sender: milyng@netcom9.netcom.com
  7. Organization: Hacker's Paradise, Inc.
  8. X-Newsreader: WinVN 0.99.7
  9. References: <4es3j2$3e2@news-e2a.gnn.com>
  10. Date: Sun, 4 Feb 1996 04:26:32 GMT
  11.  
  12. In article <4es3j2$3e2@news-e2a.gnn.com>, GaryJ@gnn.com says...
  13. >
  14. >Hi,
  15. >  I am trying to compile a 2 file program plus a header file 
  16. > using MSVC++ 1.52.I am having a problem with link error 
  17. >2025. I am using an array of structures called golf with a 
  18. >variable g.Error 2025 says that struct golf_near* g near is 
  19. >defined more than once.I would appreciate any help I could 
  20. >get.
  21.  
  22. Sounds like you're defining the array in the header file and include it in 
  23. both source files.  The correct way to do this is:
  24.  
  25. /* header.h */
  26. extern golf g[GOLF_COUNT];
  27.  
  28.  
  29. /* source1.c */
  30. #include "header.h"
  31.  
  32. golf g[GOLF_COUNT];   /* actual definition - allocates memory */
  33.  
  34.  
  35. /* source2.c */
  36. #include "header.h"
  37.  
  38. /* do nothing - golf is already defined by the header */
  39.  
  40.  
  41. An alternate method, which you may need to use because some compiler complain, 
  42. is to do:
  43.  
  44. /* header.h */
  45.  
  46. #define GOLF_SIZE  100
  47.  
  48.  
  49. /* source1.c */
  50.  
  51. golf g[GOLF_COUNT];   /* actual definition - allocates memory */
  52.  
  53.  
  54. /* source2.c */
  55.  
  56. extern golf g[GOLF_COUNT];
  57.  
  58.  
  59. Most of all, the problem seems to be that you have a global variable.  
  60. Consider defining and accessing g in one module only and let the other module 
  61. access g through the first module (using access functions).
  62.  
  63.  
  64. Mikael
  65.  
  66.  
  67.